//
//  ShoppingView.m
//  ShopDemo
//
//  Created by 飞扬的青春 on 2018/7/15.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "ShoppingView.h"

@implementation ShoppingView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.shopNum = 1;
        UIImageView *imagevi = [UIImageView new];
        self.titimagevi = imagevi;
        [self addSubview:imagevi];
        imagevi.sd_layout
        .topSpaceToView(self, 10*kJLXWidthScale)
        .leftSpaceToView(self, 10*kJLXWidthScale)
        .widthIs(60*kJLXWidthScale)
        .heightIs(90*kJLXWidthScale);
        
        UILabel *lab = [UILabel new];
        self.titlab = lab;
        lab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
        lab.numberOfLines = 2;
        lab.textColor = [UIColor blackColor];
        [self addSubview:lab];
        lab.sd_layout
        .topSpaceToView(self, 10*kJLXWidthScale)
        .leftSpaceToView(imagevi, 10*kJLXWidthScale)
        .rightSpaceToView(self, 50*kJLXWidthScale)
        .heightIs(50*kJLXWidthScale);
        
        UILabel *savelab = [UILabel new];
        self.savelab = savelab;
        savelab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
        savelab.textColor = [UIColor grayColor];
        savelab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:savelab];
        savelab.sd_layout
        .topSpaceToView(lab, 0)
        .rightSpaceToView(self, 10*kJLXWidthScale)
        .leftSpaceToView(imagevi, 10*kJLXWidthScale)
        .heightIs(20*kJLXWidthScale);
        
        UILabel *pricelab = [UILabel new];
        self.pricelab = pricelab;
        pricelab.font = [UIFont systemFontOfSize:18*kJLXWidthScale];
        pricelab.textColor = [UIColor orangeColor];
        [self addSubview:pricelab];
        pricelab.sd_layout
        .topSpaceToView(savelab, 5*kJLXWidthScale)
        .leftSpaceToView(imagevi, 10*kJLXWidthScale)
        .widthIs(80*kJLXWidthScale)
        .heightIs(20*kJLXWidthScale);
        
        UILabel *onelab = [UILabel new];
        onelab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
        onelab.text = @"规格型号";
        [self addSubview:onelab];
        onelab.sd_layout
        .topSpaceToView(imagevi, 20*kJLXWidthScale)
        .leftSpaceToView(self, 10*kJLXWidthScale)
        .heightIs(20*kJLXWidthScale)
        .widthIs(150*kJLXWidthScale);
        UILabel *linelab = [UILabel new];
        linelab.backgroundColor = LineColor;
        [self addSubview:linelab];
        linelab.sd_layout
        .topSpaceToView(onelab, 5*kJLXWidthScale)
        .leftSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .heightIs(1);
        
        
        
        
//        UILabel *numlab = [UILabel new];
//        numlab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
//        numlab.textColor = [UIColor grayColor];
//        [self addSubview:numlab];
//        numlab.sd_layout
//        .topSpaceToView(linelab, 0)
//        .leftSpaceToView(self, 0)
//        .rightSpaceToView(self, 0)
//        .heightIs(20*kJLXWidthScale);
        
//        UIButton *numbtn = [UIButton buttonWithType:UIButtonTypeSystem];
//        numbtn.backgroundColor = [UIColor redColor];
//        [numbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//        numbtn.sd_layout
//        .topSpaceToView(numlab, 5*kJLXWidthScale)
//        .leftSpaceToView(self, 10*kJLXWidthScale)
//        .heightIs(30*kJLXWidthScale)
//        .widthIs(50*kJLXWidthScale);
        
        
        MineButton *confinbtn = [MineButton buttonWithType:UIButtonTypeSystem];
        confinbtn.layer.masksToBounds = YES;
        confinbtn.layer.cornerRadius = 8*kJLXWidthScale;
        [confinbtn setTitle:@"确定" forState:UIControlStateNormal];
        confinbtn.backgroundColor = [UIColor redColor];
        [confinbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [confinbtn addTarget:self action:@selector(quedingShopingButtoAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:confinbtn];
        confinbtn.sd_layout
        .bottomSpaceToView(self, 30*kJLXWidthScale)
        .leftSpaceToView(self, 10*kJLXWidthScale)
        .rightSpaceToView(self, 10*kJLXWidthScale)
        .heightIs(45*kJLXWidthScale);
        
        UIView *btnview = [UIView new];
        btnview.backgroundColor = [UIColor orangeColor];
        [self addSubview:btnview];
        btnview.sd_layout
        .bottomSpaceToView(confinbtn, 10*kJLXWidthScale)
        .rightSpaceToView(self, 10*kJLXWidthScale)
        .heightIs(30*kJLXWidthScale)
        .widthIs(94*kJLXWidthScale);
        
        for (int i = 0; i < 3 ; i ++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor whiteColor];
            [btnview addSubview:btn];
            btn.sd_layout
            .topSpaceToView(btnview, 1)
            .leftSpaceToView(btnview, 1 + 31*kJLXWidthScale*i)
            .bottomSpaceToView(btnview, 1)
            .widthIs(30*kJLXWidthScale);
            [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(numButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            if (i == 1) {
                NSString *numStr = [NSString stringWithFormat:@"%ld",self.shopNum];
                [btn setTitle:numStr forState:UIControlStateNormal];
                btn.userInteractionEnabled = NO;
            }else if (i == 0){
//                btnview.userInteractionEnabled = YES;
                 [btn setTitle:@"-" forState:UIControlStateNormal];
            }else{
//                btnview.userInteractionEnabled = YES;
                 [btn setTitle:@"+" forState:UIControlStateNormal];
            }
            btn.tag = 250 + i;
        }
        
        
       
        
        UIButton *deltbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [deltbtn setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
        [deltbtn addTarget:self action:@selector(deleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:deltbtn];
        deltbtn.sd_layout
        .topSpaceToView(self, 5*kJLXWidthScale)
        .rightSpaceToView(self, 10*kJLXWidthScale)
        .heightIs(25*kJLXWidthScale)
        .widthIs(25*kJLXWidthScale);
        
        
        UIView *ggview = [UIView new];
//        ggview.backgroundColor = [UIColor greenColor];
        self.ggbaview = ggview;
        [self addSubview:ggview];
        ggview.sd_layout
        .topSpaceToView(linelab, 8*kJLXWidthScale)
        .leftSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .bottomSpaceToView(btnview, 10*kJLXWidthScale);
    }
    return self;
}

-(void)deleButtonAction:(MineButton *)sender{
    self.superview.hidden = YES;
    [self.superview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
}


-(void)numButtonAction:(UIButton *)sender{
    
    if (sender.tag - 250 == 0) {
        if (self.shopNum > 1) {
            self.shopNum --;
            UIButton *btn = [self viewWithTag:251];
            NSString *numstr = [NSString stringWithFormat:@"%ld",self.shopNum];
            [btn setTitle:numstr forState:UIControlStateNormal];
        }
    }else if (sender.tag - 250 == 2){
        self.shopNum ++;
        UIButton *btn = [self viewWithTag:251];
        NSString *numstr = [NSString stringWithFormat:@"%ld",self.shopNum];
        [btn setTitle:numstr forState:UIControlStateNormal];
    }
    
}
//添加购物车确定
-(void)quedingShopingButtoAction:(UIButton *)sender{
    self.shopingBlock(self.shopNum);
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
